/*
	   _           _      _           _     _ 
	  (_)         | |    | |         (_)   | |
	   _ _   _ ___| |_ __| |_ __ ___  _  __| |
	  | | | | / __| __/ _` | '__/ _ \| |/ _` |
	  | | |_| \__ \ || (_| | | | (_) | | (_| |
	  | |\__,_|___/\__\__,_|_|  \___/|_|\__,_|
	 _/ |                                     
	|__/ 
	
	Copyright 2013 Caspar Isemer and and Eva Krueger, http://justdroid.org	
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
		http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License. 
*/

package org.justdroid.justdroid;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class SettingsMenu extends JustDroidActivity {
	
	void attachKeyListener(final Button button, final int buttonFlag) {
		button.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
				if(keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
					switch(keyCode) {
					case KeyEvent.KEYCODE_DPAD_CENTER:
					case KeyEvent.KEYCODE_ENTER:
						switch(buttonFlag) {
						case 1:
							addScreenCurtain();
							break;
						case 2:
							startSettingsColor();
							break;
						case 3:
							startSettingsFont();
							break;
						case 4:
							startSettingsVolume();
							break;
						case 5:
							startSettingsAndroid();
							break;
						case 6:
							startSettingsAbout();
							break;
						}
						break;
					}
				}
				return false;
			}
		});
	}
	
	void addScreenCurtain() {
		WindowManager windowManager = getWindowManager();
		LayoutInflater inflater = getLayoutInflater();
		LayoutParamsAndViewUtils layoutParamsAndView = ScreenCurtainFunctions.prepareForCurtainCheck(inflater);

    	ScreenCurtainFunctions appState = ((ScreenCurtainFunctions) getApplicationContext());
    	if(appState.getState()) {
    		windowManager.removeView(curtainView);
    		curtainSet = false;
    		appState.setState(false);
    	} else {
    		curtainView = layoutParamsAndView.getView();
    		windowManager.addView(curtainView, layoutParamsAndView.getLayoutParams());
    		curtainSet = true;
    		appState.setState(true);
    	}
	}
	
	void startSettingsFont() {
		Intent intent = new Intent(getApplicationContext(), SettingsFont.class);
    	startActivity(intent);
	}
	
	void startSettingsColor() {
		Intent intent = new Intent(getApplicationContext(), SettingsColor.class);
    	startActivity(intent);
	}
	
	void startSettingsVolume() {
		Intent intent = new Intent(getApplicationContext(), SettingsVolume.class);
    	startActivity(intent);
	}
	
	void startSettingsAndroid() {
		Intent intent = new Intent(Settings.ACTION_SETTINGS);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);           
        startActivity(intent);
	}
	
	void startSettingsAbout() {
		Intent intent = new Intent(SettingsMenu.this, AboutActivity.class);
        startActivity(intent);
	}
	
	/** Create the Settings menu activity **/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.settings);
		super.onCreate(savedInstanceState);
		
		/** Find UI elements **/
		Button btnSettingsScreenCurtain = (Button) findViewById(R.id.btnSettingsScreenCurtain);
		Button btnSettingsColor = (Button) findViewById(R.id.btnSettingsColor);
		Button btnSettingsFont = (Button) findViewById(R.id.btnSettingsFont);
		Button btnSettingsVolume = (Button) findViewById(R.id.btnSettingsVolume);
		Button btnSettingsAndroid = (Button) findViewById(R.id.btnSettingsAndroid);
		Button btnAbout = (Button) findViewById(R.id.btnAbout);

		attachListener(btnSettingsScreenCurtain);
		attachListener(btnSettingsColor);
		attachListener(btnSettingsFont);
		attachListener(btnSettingsVolume);
		attachListener(btnSettingsAndroid);
		attachListener(btnAbout);
		
		attachKeyListener(btnSettingsScreenCurtain, 1);
		attachKeyListener(btnSettingsColor, 2);
		attachKeyListener(btnSettingsFont, 3);
		attachKeyListener(btnSettingsVolume, 4);
		attachKeyListener(btnSettingsAndroid, 5);
		attachKeyListener(btnAbout, 6);
		
		/** If Screen Curtain button is pressed, enable or disable the screen curtain system-wide depending on current state **/
		btnSettingsScreenCurtain.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	    		addScreenCurtain();
	        }
	    });
		
		/** If Color Settings button is pressed, launch the justdroid Color Settings menu **/
		btnSettingsColor.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	        	startSettingsColor();
	        }
	    });
		
		/** If Font Settings button is pressed, launch the justdroid Font Settings menu **/
		btnSettingsFont.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	        	startSettingsFont();
	        }
	    });	
		
		/** If Volume Settings button is pressed, launch the justdroid Volume Settings menu **/
		btnSettingsVolume.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	        	startSettingsVolume();
	        }
	    });		
		
		/** If Android Settings button is pressed, launch the regular Android settings **/
		btnSettingsAndroid.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	        	startSettingsAndroid();
	        }
	    });
		
		/** If About justdroid button is pressed, launch the justdroid About activity **/
		btnAbout.setOnClickListener(new OnClickListener() {
	        public void onClick(View v) {
	        	startSettingsAbout();
	        }
	    });
		
		/** Put most everything before here **/
	}	

	@Override
	public void onResume() {
		super.onResume();
		//get the root layout
		LinearLayout layout = (LinearLayout) findViewById(R.id.settings);
		Utils.applyFontColorChanges(getApplicationContext(), layout);  Utils.applyFontSizeChanges(getApplicationContext(), layout);  Utils.applyFontTypeChanges(getApplicationContext(), layout);
	}
}
